package com.cskaoyan.service;

import com.cskaoyan.bean.BO.AdminAdminPermissionBO;
import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.VO.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.MyGetCurrentTime;
import com.cskaoyan.utils.MyPageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminRoleServiceImpl
 * @Description
 * @Author Lxz
 * @Date 2022-8-12 22:53
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService{
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    MarketPermissionsFirstMapper permissionsFirstMapper;
    @Autowired
    MarketPermissionsSecondMapper permissionsSecondMapper;
    @Autowired
    MarketPermissionsThirdMapper permissionsThirdMapper;


    @Override
    public CommonData options() {
        MarketRoleExample example = new MarketRoleExample();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);

        CommonData commonData = new CommonData();
        ArrayList<AdminRoleOptionsVO> adminRoleOptionsVOS = new ArrayList<>();
        for (int i = 0; i < marketRoles.size(); i++) {
            AdminRoleOptionsVO adminRoleOptionsVO = new AdminRoleOptionsVO();
            adminRoleOptionsVO.setValue(marketRoles.get(i).getId());
            adminRoleOptionsVO.setLabel(marketRoles.get(i).getName());
            adminRoleOptionsVOS.add(adminRoleOptionsVO);
        }
        commonData.setList(adminRoleOptionsVOS);
        commonData.setPage(1);
        commonData.setPages(1);
        commonData.setTotal(marketRoles.size());
        commonData.setLimit(marketRoles.size());
        return commonData;
    }

    @Override
    public CommonData list(java.lang.String name, BasePageInfo info) {
        MyPageHelper.startPage(info);
        MarketRoleExample example = new MarketRoleExample();
        if (info.getOrder() != null && info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketRoleExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);

        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);
        CommonData data = CommonData.data(marketRolePageInfo);

        return data;
    }

    @Override
    public MarketRole create(MarketRole marketRole) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketRole.setAddTime(currentTime);
        marketRole.setUpdateTime(currentTime);
        marketRoleMapper.insertSelective(marketRole);
        return marketRole;
    }

    @Override
    public int judgeRepeat(MarketRole marketRole) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(marketRole.getName());
        criteria.andDeletedEqualTo(false);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        int result = marketRoles.size();
        return result;
    }

    @Override
    public void update(MarketRole marketRole) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketRole.setUpdateTime(currentTime);
        marketRoleMapper.updateByPrimaryKeySelective(marketRole);
    }

    @Override
    public void delete(MarketRole marketRole) {
        marketRole.setDeleted(true);
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketRole.setUpdateTime(currentTime);
        marketRoleMapper.updateByPrimaryKey(marketRole);
    }

    @Override
    public AdminRolePermissionsVO getPermissions(Integer roleId) {
        // 总响应对象
        AdminRolePermissionsVO adminRolePermissionsVO = new AdminRolePermissionsVO();

        // 用于搜索一级列表的example
        MarketPermissionsFirstExample firstExample = new MarketPermissionsFirstExample();

        /*
         * todo-Lxz 可以改成单表的，同个type
         * @Date  13:48
         **/

        // 获取总响应的systemPermissions
        List<MarketPermissionsFirst> marketPermissionsFirstList = permissionsFirstMapper.selectByExample(firstExample);
        // 一级List
        ArrayList<AdminRoleSystemPermissionsFirstVO> permissionsFirstVOArrayList = new ArrayList<>();


        //获取 一级所对应的二级列表children
        for (MarketPermissionsFirst marketPermissionsFirst : marketPermissionsFirstList) {
            Integer firstId = marketPermissionsFirst.getId();
            MarketPermissionsSecondExample secondExample = new MarketPermissionsSecondExample();
            secondExample.createCriteria().andPidEqualTo(firstId);

            // 二级List
            ArrayList<AdminRoleSystemPermissionsSecondVO> permissionsSecondVOArrayList = new ArrayList<>();

            List<MarketPermissionsSecond> marketPermissionsSecondList = permissionsSecondMapper.selectByExample(secondExample);
            for (MarketPermissionsSecond marketPermissionsSecond : marketPermissionsSecondList) {
                Integer secondId = marketPermissionsSecond.getId();

                MarketPermissionsThirdExample thirdExample = new MarketPermissionsThirdExample();
                thirdExample.createCriteria().andPidEqualTo(secondId);

                // 三级List
                ArrayList<AdminRoleSystemPermissionsThirdVO> permissionsThirdVOArrayList = new ArrayList<>();
                List<MarketPermissionsThird> marketPermissionsThirdList = permissionsThirdMapper.selectByExample(thirdExample);
                // 获取 二级所对应的三级列表children
                for (MarketPermissionsThird marketPermissionsThird : marketPermissionsThirdList) {
                    AdminRoleSystemPermissionsThirdVO permissionsThirdVO = new AdminRoleSystemPermissionsThirdVO();
                    permissionsThirdVO.setId(marketPermissionsThird.getNameid());
                    permissionsThirdVO.setLabel(marketPermissionsThird.getLabel());
                    permissionsThirdVO.setApi(marketPermissionsThird.getApi());
                    permissionsThirdVOArrayList.add(permissionsThirdVO);
                }
                AdminRoleSystemPermissionsSecondVO permissionsSecondVO = new AdminRoleSystemPermissionsSecondVO();
                // set 二级列表的 id,label以及children
                permissionsSecondVO.setId(marketPermissionsSecond.getNameid());
                permissionsSecondVO.setLabel(marketPermissionsSecond.getLabel());
                permissionsSecondVO.setChildren(permissionsThirdVOArrayList);
                permissionsSecondVOArrayList.add(permissionsSecondVO);
            }
            // set 一级列表的 id,label以及children
            AdminRoleSystemPermissionsFirstVO permissionsFirstVO = new AdminRoleSystemPermissionsFirstVO();
            permissionsFirstVO.setId(marketPermissionsFirst.getNameid());
            permissionsFirstVO.setLabel(marketPermissionsFirst.getLabel());
            permissionsFirstVO.setChildren(permissionsSecondVOArrayList);
            permissionsFirstVOArrayList.add(permissionsFirstVO);
        }
        // set 总响应的 systemPermission
        adminRolePermissionsVO.setSystemPermissions(permissionsFirstVOArrayList);


        // 获取总响应的assignedPermissions
        MarketPermissionExample permissionExample = new MarketPermissionExample();
        permissionExample.createCriteria().andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissionList = permissionMapper.selectByExample(permissionExample);
        // assignedPermissions的List
        ArrayList<AdminRoleAssignedPermissionsVO> assignedPermissionsVOList = new ArrayList<>();

        if (marketPermissionList.size() == 1) {
            java.lang.String allPermissions = "*";
            if (allPermissions.equals(marketPermissionList.get(0).getPermission())) {
                List<MarketPermissionsThird> marketPermissionsThirdList = permissionsThirdMapper.selectByExample(new MarketPermissionsThirdExample());
                for (MarketPermissionsThird marketPermissionsThird : marketPermissionsThirdList) {
                    AdminRoleAssignedPermissionsVO assignedPermissionsVO = new AdminRoleAssignedPermissionsVO();
                    assignedPermissionsVO.setNameId(marketPermissionsThird.getNameid());
                    assignedPermissionsVOList.add(assignedPermissionsVO);
                }
            }
        } else {
            for (MarketPermission marketPermission : marketPermissionList) {
                AdminRoleAssignedPermissionsVO assignedPermissionsVO = new AdminRoleAssignedPermissionsVO();
                assignedPermissionsVO.setNameId(marketPermission.getPermission());
                assignedPermissionsVOList.add(assignedPermissionsVO);
            }
        }

        /*
         * todo-Lxz  olletction.toArray()方法试试
         * @Date  22:25
         **/
        String[] assignedPermissionsVOArray = new String[assignedPermissionsVOList.size()];
        for (int i = 0; i < assignedPermissionsVOArray.length; i++) {
            assignedPermissionsVOArray[i] = assignedPermissionsVOList.get(i).getNameId();
        }
        adminRolePermissionsVO.setAssignedPermissions(assignedPermissionsVOArray);

        return adminRolePermissionsVO;
    }



    @Override
    public void postPermisssions(AdminAdminPermissionBO permissionBO) {
        Integer roleId = permissionBO.getRoleId();
        String[] permissions = permissionBO.getPermissions();
        MarketPermission marketPermission = new MarketPermission();
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketPermission.setAddTime(currentTime);
        marketPermission.setUpdateTime(currentTime);
        marketPermission.setRoleId(roleId);

        MarketPermissionExample example = new MarketPermissionExample();
        example.createCriteria().andRoleIdEqualTo(marketPermission.getRoleId());
        // 先删后增
        permissionMapper.deleteByExample(example);


        /*
         * todo-Lxz  配置文件-permissionKinds
         * @Date  22:27
         **/
        // 全权限就用*代替
        int permissionKinds = 103;
        if (permissions.length == permissionKinds) {
            String allKindsPermissions = "*";
            marketPermission.setPermission(allKindsPermissions);
            permissionMapper.insertSelective(marketPermission);
            return;
        }

        for (int i = 0; i < permissions.length; i++) {
            marketPermission.setPermission(permissions[i]);
            permissionMapper.insertSelective(marketPermission);
        }
    }
}
